Imports System.Text.RegularExpressions
Imports System.Text
Imports System.IO

Public Class Form1

    'Function GetGroupNames(ByVal pattern As String, ByVal explicit As Boolean) As String()

    '    Const GROUP_PATTERN As String = "[(](?![?]:)(?:[?]<(?<groupname>.*?)>)?.*?[)]"
    '    Const EXPLICIT_GROUP_PATTERN As String = "[(][?]<(?<groupname>.*?)>.*?[)]"

    '    Static GROUP_REGEX As New Regex(GROUP_PATTERN)
    '    Static EXPLICIT_GROUP_REGEX As New Regex(EXPLICIT_GROUP_PATTERN)

    '    Dim regex As Regex = IIf(explicit, EXPLICIT_GROUP_REGEX, GROUP_REGEX)
    '    Dim mc As MatchCollection = regex.Matches(pattern)
    '    Dim names As String() = New String(mc.Count - 1) {}
    '    For i As Integer = 0 To mc.Count - 1
    '        names(i) = mc(i).Groups("groupname").Value
    '    Next

    '    Return names

    'End Function

    Function Report(ByVal text As String, ByVal pattern As String, ByVal options As RegexOptions) As String

        'Dim groupnames As String() = GetGroupNames(pattern, options And RegexOptions.ExplicitCapture)
        Dim sb As New StringBuilder()
        Dim re As New Regex(pattern, options)
        Dim m As Match = re.Match(text)
        While m.Success
            For groupIndex As Integer = 0 To m.Groups.Count - 1
                Dim name As String = re.GroupNameFromNumber(groupIndex)
                sb.AppendLine("group <" & name & ">")

                With m.Groups(groupIndex)
                    For captureIndex As Integer = 0 To .Captures.Count - 1
                        sb.AppendLine("capture " & captureIndex & " = " & .Captures(captureIndex).Value)
                    Next
                End With

                sb.AppendLine()
            Next
            m = m.NextMatch()
        End While

        Return sb.ToString()

    End Function

    Private Function GetOptions() As RegexOptions

        Dim options As RegexOptions = RegexOptions.None

        If IgnoreCaseCheckBox.Checked Then
            options = options Or RegexOptions.IgnoreCase
        End If
        If MultilineCheckBox.Checked Then
            options = options Or RegexOptions.Multiline
        End If
        If ExplicitCaptureCheckBox.Checked Then
            options = options Or RegexOptions.ExplicitCapture
        End If
        If CompiledCheckBox.Checked Then
            options = options Or RegexOptions.Compiled
        End If
        If SingleLineCheckBox.Checked Then
            options = options Or RegexOptions.Singleline
        End If
        If IgnorePatternWhitespaceCheckBox.Checked Then
            options = options Or RegexOptions.IgnorePatternWhitespace
        End If
        If RightToLeftCheckBox.Checked Then
            options = options Or RegexOptions.RightToLeft
        End If
        If ECMAScriptCheckBox.Checked Then
            options = options Or RegexOptions.ECMAScript
        End If
        If CultureInvariantCheckBox.Checked Then
            options = options Or RegexOptions.CultureInvariant
        End If

        Return options

    End Function

    Private Sub MatchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MatchButton.Click

        Dim pattern As String = PatternTextBox.Text
        Dim text As String = TextTextBox.Text
        Dim options As RegexOptions = GetOptions()

        ReportTextBox.Text = Report(text, pattern, options)

    End Sub

    Private Sub MatchFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MatchFileButton.Click

        Dim pattern As String = PatternTextBox.Text
        Dim options As RegexOptions = GetOptions()

        With OpenFileDialog1
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim text As String = New StreamReader(.FileName).ReadToEnd()

                ReportTextBox.Text = Report(text, pattern, options)
                TextTextBox.Text = text
            End If
        End With
    End Sub
End Class
